<?php
namespace app\bancard\controller;

use app\bancard\model\DeviceAssociation;
use think\Db;
use think\facade\Cache;
use app\common\validate\ScreenSaverValidate;
use app\bancard\model\ScreenSaver as ScreenSaverModel;

class Screensaver extends Common
{
    
    //首页
    public function index()
    {
        return $this->fetch();
    }
    //列表
    public function lists()
    {
        $type = request()->param('type');
        $ntitle =request()->param('keywords');
        $title = isset($ntitle)?$ntitle:'';
        $limit = request()->param('limit');
        $offset = request()->param('offset');
        $start = ($offset-1)*$limit;
        $user_id = session('admin.id');
        $school_id = session('schoolInfo.id');
        $map =[
            'type'=>$type,
            'user_id'=>$user_id,
            'school_id'=>$school_id
        ];
        if ($title) {
            $count = db('screen_saver')->where($map)->whereLike('title', "%$title%")->count();
            $result = db('screen_saver')->where($map)->whereLike('title', "%$title%")->limit($start, $limit)->order('id desc,sort desc')->select();
        } else {
            $count = db('screen_saver')->where($map)->count();
            $result = db('screen_saver')->where($map)->limit($start, $limit)->order('id desc,sort desc')->select();
        }
        
        
        
        if ($type == 2) {
            foreach ($result as $k=>$v) {
                if (!empty($v['images'])) {
                    $n = explode(',', $v['images']);
                    foreach ($n as $key=>$value) {
                        $result[$k]['pics'][$key]['thumb'] = $value;
                        $result[$k]['pics'][$key]['src'] = $value;
                        $result[$k]['pics'][$key]['pid'] = $k.$key;
                    }
                }
            }
        }
		// p($result);die;
        return showTable(0, 'OK', $count, $result, 200);
    }
    
    //文字模式编辑
    public function edit()
    {
		if (request()->isPost()) {
		    $data = request()->param();
            if($data['start_date']){
                $date = explode(" ~ ",$data['start_date']);
                $data['start_date'] =strtotime($date[0]);
                $data['end_date'] =strtotime($date[1]);
            }
            if($data['start_time']){
                $time = explode(" ~ ",$data['start_time']);
                $data['start_time'] =$time[0];
                $data['end_time'] =$time[1];
            }
            $data['updated_at']=date('Y-m-d H:i:s', time());
		    $validate = new ScreenSaverValidate;
		    if (!$validate->scene('edit')->check($data)) {
		        return show(0, $validate->getError(), 0, 200);
		    } else {
		        Cache::clear('screen_saver');
		        $rest = (new ScreenSaverModel())->strict(false)->where('id',$data['id'])->update($data);
                if($rest){
                    //如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
                    $class_ids = DeviceAssociation::WhereIn('screen_saver_id',$data['id'])->field('class_id')->select()->toArray();
                    $class_ids = array_filter(array_column($class_ids,'class_id'));
                    if(!empty($class_ids)){
                        $pushKey = [
                            'action' => 'screen'
                        ];
                        $res = $this->socketPush($class_ids,$pushKey);
                        return show(1, '修改成功', $res, 200);
                    }
                }
		        return show(1, '修改成功', $rest, 200);
		    }
		}
        $data = request()->param();
        $result = db('screen_saver')->where('id', '=', $data['id'])->find();
		$result['week'] = explode(',',$result['switching_data']);
		// p($result);die;
        $this->assign('result', $result);
        return $this->fetch();
    }
	//图片模式编辑
	public function editImg()
	{
		if (request()->isPost()) {
		    $data = request()->param();

		    $validate = new ScreenSaverValidate;
		    if (!$validate->scene('editVideo')->check($data)) {
		        return show(0, $validate->getError(), 0, 200);
		    } else {
                if($data['start_date']){
                    $date = explode(" ~ ",$data['start_date']);
                    $data['start_date'] =strtotime($date[0]);
                    $data['end_date'] =strtotime($date[1]);
                }
                if($data['start_time']){
                    $time = explode(" ~ ",$data['start_time']);
                    $data['start_time'] =$time[0];
                    $data['end_time'] =$time[1];
                }
                $data['updated_at']=date('Y-m-d H:i:s', time());
                $data['images'] =  implode(",", $data['images']);
		        Cache::clear('screen_saver');
		        $rest = (new ScreenSaverModel())->strict(false)->where('id',$data['id'])->update($data);
                if($rest){
                    //如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
                    $class_ids = DeviceAssociation::WhereIn('screen_saver_id',$data['id'])->field('class_id')->select()->toArray();
                    $class_ids = array_filter(array_column($class_ids,'class_id'));
                    if(!empty($class_ids)){
                        $pushKey = [
                            'action' => 'screen'
                        ];
                        $res = $this->socketPush($class_ids,$pushKey);
                        return show(1, '修改成功', $res, 200);
                    }
                }
		        return show(1, '修改成功', $rest, 200);
		    }
		}
	    $data = request()->param();
	    $result = db('screen_saver')->where('id', '=', $data['id'])->find();
		$result['week'] = explode(',',$result['switching_data']);	
	    $this->assign('result', $result);
	    return $this->fetch();
	}
    //文字模式添加
    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param();

            $data['created_at'] = date('Y-m-d H:i:s', time());
            $data['user_id'] = session('admin.id');
            $data['school_id'] = session('schoolInfo.id');
            if($data['start_date']){
                $date = explode(" ~ ",$data['start_date']);
                $data['start_date'] =strtotime($date[0]);
                $data['end_date'] =strtotime($date[1]);
            }
			if($data['start_time']){
                $time = explode(" ~ ",$data['start_time']);
                $data['start_time'] =$time[0];
                $data['end_time'] =$time[1];
            }
			$data['updated_at']=date('Y-m-d H:i:s', time());
            $validate = new ScreenSaverValidate;
            if (!$validate->scene('add')->check($data)) {
                return show(0, $validate->getError(), 0, 200);
            } else {
                Cache::clear('screen_saver');
                $rest = (new ScreenSaverModel())->strict(false)->insert($data);
                return show(1, '添加成功', $rest, 200);
            }
        }
		$egetClass = $this->getClass();
		$this->assign('egetClass', json_encode($egetClass['data']));
        return $this->fetch();
    }
//    图片模式添加
    public function addImg()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $validate = new ScreenSaverValidate;
            if (!$validate->scene('addVideo')->check($data)) {
                return show(0, $validate->getError(), 0, 200);
            } else {
                $data['created_at'] = date('Y-m-d H:i:s', time());
                $data['user_id'] = session('admin.id');
                $data['school_id'] = session('schoolInfo.id');
                if($data['start_date']){
                    $date = explode(" ~ ",$data['start_date']);
                    $data['start_date'] =strtotime($date[0]);
                    $data['end_date'] =strtotime($date[1]);
                }
                if($data['start_time']){
                    $time = explode(" ~ ",$data['start_time']);
                    $data['start_time'] =$time[0];
                    $data['end_time'] =$time[1];
                }
                $data['updated_at'] =  date('Y-m-d H:i:s', time());
                $data['images'] =  implode(",", $data['images']);
                Cache::clear('screen_saver');
                $rest = (new ScreenSaverModel())->strict(false)->insert($data);
                return show(1, '添加成功', $rest, 200);
            }
        }
		$egetClass = $this->getClass();
		$this->assign('egetClass', json_encode($egetClass['data']));
        return $this->fetch();
    }
    
    //批量删除
    public function delete()
    {
        Cache::clear('screen_saver');
        $data = request()->param();
        $result = db('screen_saver')->where(['user_id'=>$data['user_id']])->delete($data['ids']);
        return show(1, '删除成功', $result, 200);
    }
    //排序
    public function sort()
    {
        $data = request()->param();
        
        $validate = new ScreenSaverValidate;
        
        if (!$validate->scene('sort')->check($data)) {
            return show(config('code.error'), 'error', $validate->getError(), 200);
        } else {
            Cache::clear('screen_saver');
            $rest = (new ScreenSaverModel())->save(['sort'=>$data['sort']], ['id'=>$data['id']]);
            return show(config('code.success'), 'OK', $rest, 200);
        }
    }
}
